/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.ui.IMainFrame;
import ro.dta.idbi.model.dynamicpattern.DependencyGraph;
import ro.dta.idbi.model.dynamicpattern.ParseException;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

/**
 * Dependency graph generator dialog, used to generate and visualise the
 * dependency graph of a transaction
 * 
 * @author Tudor Dobrila
 * 
 */
public class DepGraphDialog extends JDialog {

	private static final long serialVersionUID = 1059444988520168910L;

	/**
	 * Dependency graph visualiser dialog box
	 * 
	 * @param parentFrame
	 *            Parent frame of the application
	 */
	public DepGraphDialog(final IMainFrame parentFrame) {
		super();
		setSize(800, 600);
		setTitle("Dependency graph visualiser");
		setModal(true);
		setLocationRelativeTo(null);
		setIconImage(parentFrame.getIconImage());

		final mxGraphComponent comp = new mxGraphComponent(new mxGraph());
		comp.getViewport().setBackground(Color.WHITE);

		final JPanel centerPanel = new JPanel(new GridBagLayout());
		GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;

		constr.gridx = 0;
		constr.gridy = 0;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.ipady = 20;
		constr.gridwidth = 3;
		centerPanel.add(parentFrame.getGuiFactory().createTitleLabel("BAV Transaction:"), constr);

		constr.gridx = 0;
		constr.gridy = 1;
		constr.weightx = 1.0;
		constr.weighty = 0.4;
		final LineTextArea lineTextArea = new LineTextArea();
		centerPanel.add(lineTextArea, constr);

		constr.gridx = 0;
		constr.gridy = 2;
		constr.gridwidth = 1;
		constr.weightx = 0.33;
		constr.weighty = 0.0;
		JButton computeBtn = new JButton("Ungrouped dependency graph");
		computeBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String transStr = lineTextArea.getTextArea().getText();
				if (!"".equals(transStr)) {
					String[] trans = transStr.split("\n");
					DependencyGraph graph;
					try {
						graph = new DependencyGraph(trans);
						graph.performLayout();
						comp.setGraph(graph);
						comp.invalidate();
						comp.refresh();
					} catch (ParseException e) {
						JOptionPane.showMessageDialog(DepGraphDialog.this, e.getMessage(), "Error",
								JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		centerPanel.add(computeBtn, constr);

		constr.gridx = 1;
		constr.gridy = 2;
		constr.weightx = 0.33;
		constr.weighty = 0.0;
		final JButton groupBtn = new JButton("Grouped dependency graph");
		groupBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String[] trans = lineTextArea.getTextArea().getText().split("\n");
				DependencyGraph graph;

				try {
					graph = new DependencyGraph(trans);
					graph.group();
					graph.performLayout();
					comp.setGraph(graph);
					comp.invalidate();
					comp.refresh();
				} catch (ParseException e) {
					JOptionPane.showMessageDialog(DepGraphDialog.this, e.getMessage(), "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		centerPanel.add(groupBtn, constr);

		constr.gridx = 2;
		constr.gridy = 2;
		constr.weightx = 0.34;
		constr.weighty = 0.0;
		JButton collapseBtn = new JButton("Collapsed dependency graph");
		collapseBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String[] trans = lineTextArea.getTextArea().getText().split("\n");
				DependencyGraph graph;

				try {
					graph = new DependencyGraph(trans);
					graph.collapse();
					graph.performLayout();
					comp.setGraph(graph);
					comp.invalidate();
					comp.refresh();
				} catch (ParseException e) {
					JOptionPane.showMessageDialog(DepGraphDialog.this, e.getMessage(), "Error",
							JOptionPane.ERROR_MESSAGE);
				}

			}
		});
		centerPanel.add(collapseBtn, constr);

		constr.gridwidth = 3;
		constr.gridx = 0;
		constr.gridy = 3;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.ipady = 20;
		centerPanel.add(parentFrame.getGuiFactory().createTitleLabel("Dependency graph:"), constr);

		constr.gridx = 0;
		constr.gridy = 4;
		constr.weightx = 1.0;
		constr.weighty = 0.6;
		centerPanel.add(comp, constr);

		getContentPane().add(centerPanel, BorderLayout.CENTER);
	}

}
